#region System
using System;
using System.Collections.Generic;
using System.IO;
using CF.Framework.Engine.Drawing;
using CF.Framework.Engine.Misc;
using CF.Framework.Engine.Screens;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
#endregion

namespace CF.Framework.Engine.Controls
{
	public class Label : Control
	{
		public string Text { get; set; }
		public SpriteFont Font { get; set; }
		public Color Color { get; set; }
		public Color BorderColor { get; set; }
		public BorderStyle BorderStyle { get; set; }
		public int BorderWidth { get; set; }

		public override int Width
		{
			get
			{
				if (Font != null
					&& !string.IsNullOrEmpty(this.Text))
				{
					return (int)Font.MeasureString(this.Text).X;
				}

				return 0;
			}
			set
			{
			}
		}

		public override int Height
		{
			get
			{
				if (Font != null
					&& !string.IsNullOrEmpty(this.Text))
				{
					return (int)Font.MeasureString(this.Text).Y;
				}

				return 0;
			}
			set
			{
			}
		}

		public Label()
		{
			Color = Color.White;
			BorderStyle = BorderStyle.Shadow;
			BorderColor = Color.Black;
			BorderWidth = 1;
		}

		protected override void OnDraw(GameTime gameTime, SpriteBatch spriteBatch)
		{
			base.OnDraw(gameTime, spriteBatch);

			if (string.IsNullOrEmpty(Text))
			{
				return;
			}

			if (BorderStyle != Controls.BorderStyle.None
				&& BorderWidth > 0)
			{
				if (BorderStyle == Controls.BorderStyle.Shadow)
				{
					DrawShadow(gameTime, spriteBatch);
				}
				else if (BorderStyle == Controls.BorderStyle.Outer)
				{
					DrawOuter(gameTime, spriteBatch);
				}
			}

			if (OpacityMask != null)
			{
				spriteBatch.DrawString(Font, Text, this.ScaledPosition, Color, 0, Vector2.Zero, this.Scale, SpriteEffects.None, 0);

				if (Opacity != 255)
				{
					spriteBatch.Draw(this.OpacityMask, this.ScaledPosition, this.ScaledRectangle, new Color(255, 255, 255, (byte)MathHelper.Clamp(this.Opacity, 0, 255)), 0, Vector2.Zero, this.Scale, SpriteEffects.None, 0);
				}
			}
			else
			{
				spriteBatch.DrawString(Font, Text, this.ScaledPosition, new Color(Color.R, Color.G, Color.B, (byte)MathHelper.Clamp(this.Opacity, 0, 255)), 0, Vector2.Zero, this.Scale, SpriteEffects.None, 0);
			}
		}

		private void DrawShadow(GameTime gameTime, SpriteBatch spriteBatch)
		{
			for (var pos = 1; pos <= BorderWidth; pos++)
			{
				spriteBatch.DrawString(Font, Text, Position + new Vector2(pos, pos), BorderColor);
			}
		}

		private void DrawOuter(GameTime gameTime, SpriteBatch spriteBatch)
		{
			var position = this.ScaledPosition;

			for (var pos = 1; pos <= BorderWidth; pos++)
			{
				spriteBatch.DrawString(Font, Text, position + new Vector2(-pos, -pos), BorderColor);
				spriteBatch.DrawString(Font, Text, position + new Vector2(-pos, pos), BorderColor);
				spriteBatch.DrawString(Font, Text, position + new Vector2(pos, -pos), BorderColor);
				spriteBatch.DrawString(Font, Text, position + new Vector2(pos, pos), BorderColor);
			}
		}
	}
}
